package com.zy.xuanwo.acitivity;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zy.xuanwo.R;
import com.zy.xuanwo.fragment.AboutFragment;
import com.zy.xuanwo.fragment.DeviceFragment;
import com.zy.xuanwo.fragment.SettingFragment;
import com.zy.xuanwo.utils.SharedPreferencesUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends FragmentActivity implements View.OnClickListener {


    @Bind(R.id.main_fragment)
    FrameLayout mainFragment;
    @Bind(R.id.main_device)
    TextView mainDevice;
    @Bind(R.id.main_setting)
    TextView mainSetting;
    @Bind(R.id.main_about)
    TextView mainAbout;
    @Bind(R.id.ll_maintab)
    LinearLayout llMaintab;
    private AboutFragment mAboutFragment;
    private DeviceFragment mDeviceFragment;
    private SettingFragment mSettingFragment;
    private FragmentManager fragmentManager;

    private boolean isExit;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        //默认选择的Fragment
        changeDeviceStyle();
            fragmentManager = getSupportFragmentManager();
            mDeviceFragment = new DeviceFragment();
            fragmentManager.beginTransaction().replace(R.id.main_fragment, mDeviceFragment).commit();
        initUserSetting();
    }

    private void initUserSetting() {
        SharedPreferencesUtils.put(this,SharedPreferencesUtils.REMEMBER_PASSWORD,SharedPreferencesUtils.CHECKED);
        SharedPreferencesUtils.put(this,SharedPreferencesUtils.AUTO_LOGIN,SharedPreferencesUtils.CHECKED);
    }

    /**
     * 清除所有样式
     */
    public void clearAllStyle() {
        mainDevice.setTextColor(Color.parseColor("#999999"));
        Drawable drawable = getResources().getDrawable(R.drawable.devicelist_btn_wifidevice_n);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        mainDevice.setCompoundDrawables(null, drawable, null, null);

        mainSetting.setTextColor(Color.parseColor("#999999"));
        Drawable drawable1 = getResources().getDrawable(R.drawable.devicelist_btn_setting_n);
        drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
        mainSetting.setCompoundDrawables(null, drawable1, null, null);

        mainAbout.setTextColor(Color.parseColor("#999999"));
        Drawable drawable3 = getResources().getDrawable(R.drawable.devicelist_btn_moreee_n);
        drawable3.setBounds(0, 0, drawable3.getMinimumWidth(), drawable3.getMinimumHeight());
        mainAbout.setCompoundDrawables(null, drawable3, null, null);
    }


    /**
     * 判断fragment是否显示
     *
     * @param transaction
     */
    public void hideAllFragment(FragmentTransaction transaction) {
        if (mDeviceFragment != null) {
            transaction.hide(mDeviceFragment);
        }

        if (mSettingFragment != null) {
            transaction.hide(mSettingFragment);
        }


        if (mAboutFragment != null) {
            transaction.hide(mAboutFragment);
        }

    }


    private void changeDeviceStyle() {
        mainDevice.setTextColor(Color.parseColor("#01B9FF"));
        Drawable drawable = getResources().getDrawable(R.drawable.devicelist_btn_wifidevice_p);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        mainDevice.setCompoundDrawables(null, drawable, null, null);
    }

    private void changeSettingStyle() {
        mainSetting.setTextColor(Color.parseColor("#01B9FF"));
        Drawable drawable = getResources().getDrawable(R.drawable.devicelist_btn_setting_p);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        mainSetting.setCompoundDrawables(null, drawable, null, null);
    }

    private void changeAboutStyle() {
        mainAbout.setTextColor(Color.parseColor("#01B9FF"));
        Drawable drawable = getResources().getDrawable(R.drawable.devicelist_btn_moreee_p);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        mainAbout.setCompoundDrawables(null, drawable, null, null);
    }

    @OnClick({R.id.main_device, R.id.main_setting, R.id.main_about})
    public void onClick(View view) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        hideAllFragment(transaction);

        clearAllStyle();

        switch (view.getId()) {
            case R.id.main_device:
                changeDeviceStyle();
                if (mDeviceFragment == null) {
                    mDeviceFragment = new DeviceFragment();
                    transaction.add(R.id.main_fragment, mDeviceFragment);
                } else {
                    transaction.show(mDeviceFragment);
                }
                break;
            case R.id.main_setting:
                changeSettingStyle();

                if (mSettingFragment == null) {
                    mSettingFragment = new SettingFragment();
                    transaction.add(R.id.main_fragment, mSettingFragment);
                } else {
                    transaction.show(mSettingFragment);
                }

                break;
            case R.id.main_about:
                changeAboutStyle();
                if (mAboutFragment == null) {
                    mAboutFragment = new AboutFragment();
                    transaction.add(R.id.main_fragment, mAboutFragment);
                } else {
                    transaction.show(mAboutFragment);
                }
                break;
        }
        transaction.commit();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            this.finish();
        }
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }

    };

}
